# Create your models here.
from django.db import models
from rest_framework import serializers

from monitor_alert_center.models import MonitorGroup
from user.models import SysUser
from hosts_mgr.models import GroupList


class SysRole(models.Model):
    """系统角色表"""
    name = models.CharField(max_length=30, null=True, verbose_name="角色名称")
    code = models.CharField(max_length=100, null=True, verbose_name="角色权限字符串")
    create_time = models.DateTimeField(null=True, verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(null=True, verbose_name="更新时间", auto_now=True)
    remark = models.CharField(max_length=500, null=True, verbose_name="备注")

    class Meta:
        verbose_name = "系统角色表"
        db_table = "sys_role"


class SysRoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SysRole
        fields = '__all__'


class SysUserRole(models.Model):
    """系统用户角色关联表"""
    role = models.ForeignKey(SysRole, on_delete=models.PROTECT)
    user = models.ForeignKey(SysUser, on_delete=models.PROTECT)

    class Meta:
        verbose_name = "系统用户角色关联表"
        db_table = "sys_user_role"


class SysUserRoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SysUserRole
        fields = '__all__'


class SysRoleGroupList(models.Model):
    """系统用户角色与主机关联表"""
    role = models.ForeignKey(SysRole, on_delete=models.PROTECT)
    group = models.ForeignKey(GroupList, on_delete=models.PROTECT)

    class Meta:
        verbose_name = "系统用户角色与主机关联表"
        db_table = "sys_role_group_list"


class SysRoleMonitorGroup(models.Model):
    """角色监控组关联表"""
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE)
    group = models.ForeignKey(MonitorGroup, on_delete=models.CASCADE)

    class Meta:
        verbose_name = "角色监控组关联表"
        db_table = "sys_role_monitor_group"
